﻿using BusinessEntity;
using BusinessEntity.xmglQuerryEntity;
using BusinessRepository;
using BusinessRule.Organization;
using learun.util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;

namespace BusinessRule
{
    public class ProFuKuanRule
    {
        public static async Task<(IEnumerable<dynamic>, int)> GetFuKuanSQListDB(ProFuKuanSQQuerry querry, string strdepids, string whereappend = "")
        {
            string str = "";

            if (!string.IsNullOrEmpty(whereappend))
            {
                str = str + whereappend;
            }

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmid = ''" + querry.xmid + "''";
            }
            if (!(string.IsNullOrEmpty(querry.jsmc)))
            {
                str = str + " and buildunit = ''" + querry.jsmc + "''";
            }

            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and jf = ''" + querry.jsdw + "''";
            }

            if (!(string.IsNullOrEmpty(querry.sgdw)))
            {
                str = str + " and yf = ''" + querry.sgdw + "''";
            }

            if (!(string.IsNullOrEmpty(querry.xmmc)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.xmnf)))
            {
                str = str + " and xmnf like '%" + querry.xmnf + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.ny)))
            {
                str = str + " and SUBSTRING(CONVERT(varchar(100), sqrq, 23),1,7) = ''" + querry.ny + "''";
            }

            if (!(string.IsNullOrEmpty(querry.htid)))
            {
                str = str + " and htid like ''%" + querry.htid + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.htbh)))
            {
                str = str + " and htbh like ''%" + querry.htbh + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.htmc)))
            {
                str = str + " and htmc like '%" + querry.htmc + "%'";
            }


         
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like ''%" + querry.dxmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.fkmc)))
            {
                str = str + " and fkmc like ''%" + querry.fkmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.jhfk)))
            {
                str = str + " and jhfkx like ''%" + querry.jhfk + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.begindate.ToString())))
            {
                str = str + " and sqrq >= ''" + querry.begindate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.enddate.ToString())))
            {
                str = str + " and sqrq <= ''" + querry.enddate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = ''" + querry.status + "''";
            }


            str += " and xmid not in (select xmid from CProGD)";

            string sql = "select * from View_ProFKSQLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //查询付款申请列表
        public static async Task<(IEnumerable<dynamic>, int)> GetFuKuanSQList(ProFuKuanSQQuerry querry, string strdepids, string whereappend = "")
        {
            string str = "";

            if (!string.IsNullOrEmpty(whereappend))
            {
                str = str + whereappend;
            }

            if (!(string.IsNullOrEmpty(querry.xmid)))
            {
                str = str + " and xmid = ''" + querry.xmid + "''";
            }

            if (!(string.IsNullOrEmpty(querry.jsdw)))
            {
                str = str + " and jf = ''" + querry.jsdw + "''";
            }

            if (!(string.IsNullOrEmpty(querry.htid)))
            {
                str = str + " and htid like ''%" + querry.htid + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.htbh)))
            {
                str = str + " and htbh like ''%" + querry.htbh + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.htmc)))
            {
                str = str + " and htmc like '%" + querry.htmc + "%'";
            }


            if (!(string.IsNullOrEmpty(querry.xmmc)))
            {
                str = str + " and xmmc like '%" + querry.xmmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.dxmc)))
            {
                str = str + " and dxmc like ''%" + querry.dxmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.fkmc)))
            {
                str = str + " and fkmc like ''%" + querry.fkmc + "%'";
            }
            if (!(string.IsNullOrEmpty(querry.jhfk)))
            {
                str = str + " and jhfkx like ''%" + querry.jhfk + "%'";
            }

            if (!(string.IsNullOrEmpty(querry.begindate.ToString())))
            {
                str = str + " and sqrq >= ''" + querry.begindate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.enddate.ToString())))
            {
                str = str + " and sqrq <= ''" + querry.enddate + "''";
            }
            if (!(string.IsNullOrEmpty(querry.status)))
            {
                str = str + " and spstatus = ''" + querry.status + "''";
            }


            str += " and xmid not in (select xmid from CProGD)";

            string sql = "select * from View_ProFKSQLists where (departid in " + strdepids + " or cqdwid in " + strdepids + ")" + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }

        //查询付款申请列表
        public static async Task<(IEnumerable<dynamic>, int)> GetHTFuKuanSQList(ProFuKuanSQQuerry querry, string strdepids)
        {
            string str = "";
            if (!(string.IsNullOrEmpty(querry.htid)))
            {
                //str = str + " and htid like '%" + querry.htid + "%'";
                str = str + " and ( htid like '%" + querry.htid + "%' or htbh like '%" + querry.htid + "%')";
            }

            //str += " and xmid not in (select xmid from CProGD)";

            string sql = "select * from View_ProFKSQLists_mx where 1=1 " + str.Replace("''", "'");
            bool isAsc = true;
            if (querry.sort != "Asc" && querry.sort != "asc")
            {
                isAsc = false;
            }
            if (string.IsNullOrEmpty(querry.column))
            {
                querry.column = "id";
            }
            return await new RepositoryFactory().BaseRepository().FindList(sql, null, querry.column, isAsc, querry.size, querry.current);

        }
        //付款申请-单条
        public static async Task<IEnumerable<dynamic>> GetFKSQById(string keyValue)
        {
            string sql = "select * ,(select yf from CProContract where CProContract.htid = CProFKSQ.htid ) yf,(select jf from CProContract where CProContract.htid = CProFKSQ.htid ) jf from CProFKSQ where id = '" + keyValue + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //付款申请-根据htid获取项目信息
        public static async Task<IEnumerable<dynamic>> GetFKSQXMByhtid(string htid)
        {
            string sql = "select * from View_ProFKLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //付款申请-根据htid获取申请记录
        public static async Task<IEnumerable<dynamic>> GetFKSQJLByhtid(string htid)
        {
            string sql = "select * from View_ProFKSQLists where htid = '" + htid + "'";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }

        //删除多条付款申请
        public static async Task DeleteCProFKSQMulti(Root ids)
        {
            //foreach (BaseDeleteMulti idm in ids.ids)
            //{
            //    if (!BaseEditPower.GetEditPower(idm.id))
            //    {
            //        throw new Exception("请确认数据权限");
            //    }
            //}
            //foreach (BaseDeleteMulti idm in ids.ids)
            //{
            //    string delsql = "delete from CProFKSQ where id = '" + idm.id + "'";

            //    await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            //}

            foreach (BaseDeleteMulti idm in ids.ids)
            {
                if (!await BaseEditPower.GetEditPower(idm.id, BaseEditPower.TableName.付款申请))
                {
                    throw new Exception("请确认数据权限");
                }
            }


            await TranstionRule.GetRunActionTransAsync(async (iRep) =>
            {
                foreach (BaseDeleteMulti idm in ids.ids)
                {
                    string delsql = "delete from CProFKSQ where id = '" + idm.id + "'";
                    await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
                }
                return true;
            });

        }
        ////查询付款列表
        //public static async Task<(DataTable, int)> GetFuKuanList(ProFuKuanQuerry querry, string uid)
        //{
        //    string str = "";
        //    if (!(string.IsNullOrEmpty(querry.Jf)))
        //    {
        //        str = str + " and jf like ''%" + querry.Jf + "'";
        //    }
        //    if (!(string.IsNullOrEmpty(querry.Xmmc)))
        //    {
        //        str = str + " and xmmc like '%" + querry.Xmmc + "%'";
        //    }
        //    if (!(string.IsNullOrEmpty(querry.Yf)))
        //    {
        //        str = str + " and yf like ''%" + querry.Yf + "'";
        //    }
        //    if (!(string.IsNullOrEmpty(querry.Begindate.ToString())))
        //    {
        //        str = str + " and sqrq >= ''" + querry.Begindate + "''";
        //    }
        //    if (!(string.IsNullOrEmpty(querry.Enddate.ToString())))
        //    {
        //        str = str + " and sqrq <= ''" + querry.Enddate + "''";
        //    }
        //    if (!(string.IsNullOrEmpty(querry.Status)))
        //    {
        //        str = str;
        //    }

        //    string sql = "select * from View_ProFKLists where departid in " + await UserRule.GetUserDepid(uid) + str.Replace("''", "'");
        //    bool isAsc = true;
        //    if (querry.sort != "Asc" && querry.sort != "asc")
        //    {
        //        isAsc = false;
        //    }
        //    if (string.IsNullOrEmpty(querry.column))
        //    {
        //        querry.column = "id";
        //    }
        //    return await new RepositoryFactory().BaseRepository().FindTable(sql, null, querry.column, isAsc, querry.size, querry.current);

        //}
        //新增修改付款申请
        public static async Task InsertCProFKSQ(CProFKSQ fksq, string uid)
        {
            CProFKSQ count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProFKSQ>(fksq.id);
            if (count is null)
            {
                string strid = CommonHelper.GetUniqueId().ToString();
                fksq.id = strid;
                if (string.IsNullOrEmpty(fksq.sqje.ToString()))
                {
                    throw new Exception("申请金额不能为空");
                }
                if (string.IsNullOrEmpty(fksq.sqrq.ToString()))
                {
                    throw new Exception("申请日期不能为空");
                }
                if (string.IsNullOrEmpty(fksq.remark))
                {
                    throw new Exception("收款银行不能为空");
                }
                if (string.IsNullOrEmpty(fksq.remark2))
                {
                    throw new Exception("收款账号不能为空");
                }
                //fksq.hdje = fksq.sqje;
                if (string.IsNullOrEmpty(fksq.status))
                {
                    fksq.status = "2";
                }

                fksq.createuser = uid;
                await new RepositoryFactory().BaseRepository().Insert(fksq);
            }
            else
            {
                if (!await BaseEditPower.GetEditPower(fksq.id, BaseEditPower.TableName.付款申请))
                {
                    throw new Exception("请确认数据权限");
                }
                if (string.IsNullOrEmpty(fksq.sqje.ToString()))
                {
                    throw new Exception("申请金额不能为空");
                }
                if (string.IsNullOrEmpty(fksq.sqrq.ToString()))
                {
                    throw new Exception("申请日期不能为空");
                }
                if (string.IsNullOrEmpty(fksq.remark))
                {
                    throw new Exception("收款银行不能为空");
                }
                if (string.IsNullOrEmpty(fksq.remark2))
                {
                    throw new Exception("收款账号不能为空");
                }
                //fksq.hdje = fksq.sqje;
                if (string.IsNullOrEmpty(fksq.status))
                {
                    fksq.status = "2";
                }

                fksq.createuser = uid;
                await new RepositoryFactory().BaseRepository().Update(fksq);
            }
        }
        //付款明细-根据sqid获取付款明细
        public static async Task<IEnumerable<dynamic>> GetFKMXBysqid(string sqid)
        {
            string sql = "select * from CProFKMX where sqid = '" + sqid + "' order by zfrq desc";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //付款明细-根据fkid获取单条付款明细
        public static async Task<IEnumerable<dynamic>> GetFKMXByfkid(string fkid)
        {
            string sql = "select * from CProFKMX where id = '" + fkid + "' order by zfrq desc";
            return await new RepositoryFactory().BaseRepository().FindList(sql, null);
        }
        //新增修改付款明细
        public static async Task InsertCProFKMX(CProFKMX fkmx, string uid)
        {
            CProFKMX count = await new RepositoryFactory().BaseRepository().FindEntityByKey<CProFKMX>(fkmx.id);
            if (count is null)
            {
                //申请单已审核
                string sql = "select sqstatus,sqje,zfje from View_ProFKSQLists where id = '" + fkmx.sqid + "'";
                DataTable table = await new RepositoryFactory().BaseRepository().FindTable(sql, null);
                if (table.Rows[0]["sqstatus"].ToString() != "已审核")
                {
                    throw new Exception("暂时不能进行付款操作");
                }
                else
                {
                    if (string.IsNullOrEmpty(fkmx.zfje.ToString()))
                    {
                        throw new Exception("付款金额不能为空");
                    }
                    //支付金额
                    decimal num = fkmx.zfje.ToDecimal();
                    //已支付金额
                    decimal num2 = table.Rows[0]["zfje"].ToDecimal();
                    //支付金额大于申请金额、本次支付加上已支付大于申请金额
                    if ((num > table.Rows[0]["sqje"].ToDecimal()) || ((num + num2) > table.Rows[0]["sqje"].ToDecimal()))
                    {
                        throw new Exception("已超付！请确定");
                    }
                    string strid = CommonHelper.GetUniqueId().ToString();
                    fkmx.id = strid;
                    fkmx.createuser = uid;
                    await new RepositoryFactory().BaseRepository().Insert(fkmx);
                }
            }
            else
            {
                fkmx.createuser = uid;
                await new RepositoryFactory().BaseRepository().Update(fkmx);
            }
        }
        //删除多条付款明细
        public static async Task DeleteCProFKMXMulti(Root ids)
        {
            foreach (BaseDeleteMulti idm in ids.ids)
            {
                string delsql = "delete from CProFKMX where id = '" + idm.id + "'";

                await new RepositoryFactory().BaseRepository().ExecuteSql(delsql);
            }

        }
    }
}
